package com.example.ncpapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.ncpapplication.R;
import com.example.ncpapplication.bean.NCPInfo;
import com.example.ncpapplication.utils.MyAdapter;
import com.example.ncpapplication.utils.OkHttp;
import com.google.gson.Gson;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private GridView gd_provinceList;
    private MyAdapter adapter;
    public static List<NCPInfo.NewslistBean> ncpList;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private WebView wv_echart;
    private List<String> provinceList = new ArrayList<>();
    private List<Integer> countList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getData();
    }

    private void getTop10() {
        for (int i = 0; i < 5; i++) {
            NCPInfo.NewslistBean bean = ncpList.get(i);
            provinceList.add("'" + bean.getProvinceName() + "'");
            countList.add(bean.getConfirmedCount());
        }
        Log.d("MainActivity", Arrays.toString(provinceList.toArray()));
        Log.d("MainActivity", Arrays.toString(countList.toArray()));
        wv_echart.post(() -> wv_echart.loadUrl("javascript:showBarChart(" + Arrays.toString(provinceList.toArray()) + ", " + Arrays.toString(countList.toArray()) + ")"));
    }

    private void getData() {
        OkHttp.requestData(format.format(System.currentTimeMillis()), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("MainActivity", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                NCPInfo ncpInfo = new Gson().fromJson(response.body().string(), NCPInfo.class);
                ncpList.addAll(ncpInfo.getNewslist());
                runOnUiThread(() -> {
                    getTop10();
                    adapter.notifyDataSetChanged();
                });
            }
        });
    }

    private void initView() {
        ncpList = new ArrayList<>();
        gd_provinceList = (GridView) findViewById(R.id.gd_provinceList);
        adapter = new MyAdapter(getApplicationContext(), R.layout.province_item_layout, ncpList);
        gd_provinceList.setAdapter(adapter);
        gd_provinceList.setOnItemClickListener(this);
        wv_echart = (WebView) findViewById(R.id.wv_echart);
        wv_echart.getSettings().setJavaScriptEnabled(true);
        wv_echart.loadUrl("file:///android_asset/echart.html");
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(MainActivity.this, CityActivity.class);
        intent.putExtra("position", position + "");
        startActivity(intent);
    }
}
